package cn.tom.controller;

import cn.tom.entity.*;
import cn.tom.respjson.RespJson;
import cn.tom.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller //实例化装配到IOC容器
@RequestMapping("/api/public")   //公开的路径
public class PublicController {
    static Base64.Decoder decoder = Base64.getDecoder();    //Base64格式
    @Autowired
    InformationService informationService;              //注入InformationService接口
    @Autowired
    TeacherService teacherService;                      //注入TeacherService接口
    @Autowired
    CertificateService certificateService;              //CertificateService接口
    @Autowired
    InformationTypeService informationTypeService;      //注入InformationTypeMapper接口
    @Autowired
    ProductIpfsService productIpfsService;              //注入ProductIpfsService接口
    @Autowired
    GroupService groupService;                          //注入GroupService接口
    @Autowired
    LargefileService largefileService;
    @Autowired
    DevelopmentHistoryService developmentHistoryService;                //注入DevelopmentHistoryService接口
    @Autowired
    HeatMapService heatMapService;                                      //注入HeatMapService接口
    @Autowired
    RecruitmentService recruitmentService;                             //注入RecruitmentService接口
    @Autowired
    ElegantDemeanourService elegantDemeanourService;                   //注入ElegantDemeanourService接口
    @Autowired
    PublicInterestService publicInterestService;                        //注入PublicInterestService接口

    //Logger输出
    private Logger log
            = LoggerFactory.getLogger(cn.tom.tool.Log.class);

    /**
     * 查询全部资讯
     */
    @GetMapping("/information/findAll")
    @ResponseBody
    public Map<String, Object> findAllInformation() {
        List<Information> lst = informationService.findAll();   //执行全查询
        if (null != lst) {
            return RespJson.pack(200, "查询成功", lst);
        }
        return RespJson.pack(500, "查询失败", null);
    }


    /**
     * 根据资讯id查询
     */
    @GetMapping("/information/findById/{id}")
    @ResponseBody
    public Map<String, Object> findById(@PathVariable("id") Integer id) {
        Information byId = informationService.findById(id);     //执行根据ID查询

        if (null != byId) {
            informationService.update_page_view(byId);//执行浏览量+1
        }

        if (null != byId) {
            return RespJson.pack(200, "查询成功", byId);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /***
     *分页查询
     */
    @GetMapping("/information/findByPage/{start}/{num}/{typeId}")
    @ResponseBody
    public Map<String, Object> findByPage(
            @PathVariable("start") Integer start,
            @PathVariable("num") Integer num,
            @PathVariable("typeId") Integer typeId) {
        //获取总行数
        int total = informationService.findCount(typeId);
        //获取第n页
        int startIndex = (start - 1) * num;
        //执行分页查询
        List<Information> page = informationService.findByPage(startIndex, num, typeId);
        //创建一个map集合
        Map<String, Object> map = new HashMap<>();
        //将数据存储进map集合
        map.put("total", total);
        map.put("page", page);
        if (null != page) {
            return RespJson.pack(200, "查询成功", map);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }

    }

    /**
     * 根据时间查询最新的6条资讯
     */
    @GetMapping("/information/findByDay")
    @ResponseBody
    public Map<String, Object> findByDay() {
        List<Information> byDay = informationService.findByDay();   //执行最新资讯查询

        if (null != byDay) {
            return RespJson.pack(200, "查询成功", byDay);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }

    }

    /**
     * 资讯类型的全查询
     */
    @GetMapping("/informationType/findAll")
    @ResponseBody
    public Map<String, Object> findTypeAll() {
        List<InformationType> lst = informationTypeService.findAll();
        if (null != lst) {
            return RespJson.pack(200, "查询成功", lst);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 根据ID查询类型
     */
    @GetMapping("/informationType/findById/{id}")
    @ResponseBody
    public Map<String, Object> findById5(@PathVariable("id") Integer id) {
        InformationType byId = informationTypeService.findById(id);
        if (null != byId) {
            return RespJson.pack(200, "查询成功", byId);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 查询全部老师
     */
    @GetMapping("/teacher/findAll")
    @ResponseBody
    public Map<String, Object> findAllTeacher() {
        List<Teacher> lst = teacherService.findAll();   //执行全查询
        if (null != lst) {
            return RespJson.pack(200, "查询成功", lst);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 根据老师id查询
     */
    @GetMapping("teacher/findById/{id}")
    @ResponseBody
    public Map<String, Object> findByIdTeacher(@PathVariable("id") Integer id) {
        Teacher byId = teacherService.findById(id); //执行根据ID查询
        if (null != byId) {
            return RespJson.pack(200, "查询成功", byId);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }
    /**
     * 根据老师类型查询
     */
    @GetMapping("teacher/findByType/{id}")
    @ResponseBody
    public Map<String,Object> findByType(@PathVariable("id") String type){
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        List<Teacher> list = teacherService.list(queryWrapper);
        if(null != list){
            return RespJson.pack(200,"查询成功",list);
        }else {
            return RespJson.pack(500,"查询失败",null);
        }
    }

    /**
     * 查询最新的5条证书
     */
    @GetMapping("/certificate/findByDay")
    @ResponseBody
    public Map<String, Object> findByDay2() {
        List<Certificate> byDay = certificateService.findByDay();
        if (null != byDay) {
            return RespJson.pack(200, "查询成功", byDay);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 证书全查询
     */
    @GetMapping("/certificate/findAll")
    @ResponseBody
    public Map<String, Object> findAll() {
        List<Certificate> lst = certificateService.findAll();
        if (null != lst) {
            return RespJson.pack(200, "查询成功", lst);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 根据证书ID查询
     */
    @GetMapping("/certificate/findById/{id}")
    @ResponseBody
    public Map<String, Object> findById2(@PathVariable("id") Integer id) {
        Certificate byId = certificateService.findById(id);
        if (null != byId) {
            return RespJson.pack(200, "查询成功", byId);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 产品IPFS 全查询
     */
    @GetMapping("/productIpfs/findAll")
    @ResponseBody
    public Map<String, Object> findProductIpfsAll() {
        List<ProductIpfs> lst = productIpfsService.findAll();
        if (null != lst) {
            return RespJson.pack(200, "查询成功", lst);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 产品IPFS根据ID查询
     */
    @GetMapping("/productIpfs/findById/{id}")
    @ResponseBody
    public Map<String, Object> findProductIpfsById(@PathVariable("id") Integer id) {
        ProductIpfs byId = productIpfsService.findById(id);
        if (null != byId) {
            return RespJson.pack(200, "查询成功", byId);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 根据时间查询最新的6条团队信息
     */
    @GetMapping("/group/findByDate")
    @ResponseBody
    public Map<String, Object> findByDate() {
        List<Group> byDate = groupService.findByDate();
        if (null != byDate) {
            return RespJson.pack(200, "查询成功", byDate);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 根据团队ID查询
     */
    @GetMapping("/group/findById/{id}")
    @ResponseBody
    public Map<String, Object> findGroupById(@PathVariable("id") Integer id) {
        Group byId = groupService.findById(id);
        if (null != byId) {
            return RespJson.pack(200, "查询成功", byId);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }


    /**
     * 发展历程 奇数查询
     */
    @GetMapping("/developmentHistory/findByOdd")
    @ResponseBody
    public Map<String, Object> findByOdd() {
        List<DevelopmentHistory> byOdd = developmentHistoryService.findByOdd();
        if (null != byOdd) {
            return RespJson.pack(200, "查询成功", byOdd);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }


    /**
     * 发展历程 偶数查询
     */
    @GetMapping("/developmentHistory/findByEven")
    @ResponseBody
    public Map<String, Object> findByEven() {
        List<DevelopmentHistory> byEven = developmentHistoryService.findByEven();
        if (null != byEven) {
            return RespJson.pack(200, "查询成功", byEven);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }


    /**
     * 根据发展历程图片类型ID查询
     */
    @GetMapping("/developmentHistory/findById/{id}")
    @ResponseBody
    public Map<String, Object> findDevelopmentHistoryById(@PathVariable("id") Integer id) {
        DevelopmentHistory byId = developmentHistoryService.getById(id);
        if (null != byId) {
            return RespJson.pack(200, "查询成功", byId);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 图片全查询
     */
    @GetMapping("/largefile/findAll")
    @ResponseBody
    public Map<String, Object> findlargefileAll() {
        List<Largefile> all = largefileService.findAll();
        if (null != all) {
            return RespJson.pack(200, "查询成功", all);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 热点图全查询
     */
    @GetMapping("/heatMap/findAll")
    @ResponseBody
    public Map<String, Object> findHeatMapAll() {
        List<HeatMap> list = heatMapService.list();
        if (null != list) {
            return RespJson.pack(200, "查询成功", list);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 诚聘英才全查询
     */
    @GetMapping("/recruitment/findAll")
    @ResponseBody
    public Map<String, Object> findRecruitmentAll() {
        List<Recruitment> list = recruitmentService.list();
        if (null != list) {
            return RespJson.pack(200, "查询成功", list);
        } else {
            return RespJson.pack(500, "查询失败", null);
        }
    }

    /**
     * 公益活动 根据时间查询最新的6条公益活动
     */
    @GetMapping("/publicInterest/findByDay")
    @ResponseBody
    public Map<String,Object> findPublicInterestByDay(){
        List<PublicInterest> byId = publicInterestService.findByDay();
        if(null != byId){
            return RespJson.pack(200,"查询成功",byId);
        }else {
            return RespJson.pack(500,"查询失败",null);
        }
    }

    /**
     * 公益活动根据ID查询公益活动详情
     */
    @GetMapping("/publicInterest/findById/{id}")
    @ResponseBody
    public Map<String,Object> findByID(@PathVariable("id") Long id){
        PublicInterest byId = publicInterestService.findById(id);
        if(null != byId){
            return RespJson.pack(200,"查询成功",byId);
        }else {
            return RespJson.pack(500,"查询失败",null);
        }
    }
}
